import RequestResult = require("./RequestResult")
import RequestListResult = require("./RequestListResult")

class ResultWrapper {

    public static wrap<T>(data: T, code = 0, message = ''): RequestResult<T> {
        let result = new RequestResult<T>()
        result.code = code
        result.message = message
        result.data = data
        return result
    }

    public static wrapList<T>(list: T[], code = 0, message = ''): RequestListResult<T> {
        let result = new RequestListResult<T>()
        result.code = code
        result.message = message
        result.data = list
        return result
    }

    public static fakeList(model: {new (): any}, page = 1, page_size = 20) {
        let list: any[] = []
        for (let i = 0; i < (page * page_size); i++) {
            list.push(new model())
        }
        return list
    }

    public static success(message: string): RequestResult<any> {
        return ResultWrapper.wrap({}, 0, message)
    }

    public static failure(message: string, code = 1): RequestResult<any> {
        return ResultWrapper.wrap({}, code, message)
    }

    public static successList(message: string): RequestListResult<any> {
        return ResultWrapper.wrapList([], 0, message)
    }

    public static failureList(message: string, code = 1): RequestListResult<any> {
        return ResultWrapper.wrapList([], code, message)
    }
}
export = ResultWrapper